﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using Runda.EShop.Common.Utility;
using Runda.EShop.Common.Entity;
using System.Data.SqlClient;

namespace Runda.EShop.Dal
{
    /// <summary>
    /// DAL layer class dbo.Biz_ShopCart.
    /// </summary>
    public partial class BizShopCartDal : DalBase
    {
        public DataTable GetShopcartByCustomerID(Guid CustomerID)
        {
            string sql = @"select A.CartID,A.CustomerID,A.ProductID,A.Quantity,A.CreatedAt,B.ProductName,B.ProductCode,B.Standard,B.Unit
,A.BatchCode,A.DueDate,A.Inventory,A.Remark
FROM Biz_ShopCart A JOIN  Bas_Product B
ON A.ProductID = B.ProductID WHERE A.CustomerID = @CustomerID";

            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@CustomerID", DbType.Guid, CustomerID);
            return CurrentDatabase.ExecuteDataSet(dbcommand).Tables[0];
        }

        /// <summary>
        /// 修改订单数量
        /// </summary>
        /// <param name="list"></param>
        public void ModifyShopcart(BizShopCartEntity entity)
        {
            string sql = "UPDATE Biz_ShopCart SET Quantity = @Quantity,Unit=@Unit WHERE CartID =@CartID";
            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@CartID", DbType.Guid, entity.CartID);
            CurrentDatabase.AddInParameter(dbcommand, "@Quantity", DbType.Decimal, entity.Quantity);
            CurrentDatabase.AddInParameter(dbcommand, "@Unit", DbType.String, entity.Unit);

            CurrentDatabase.ExecuteNonQuery(dbcommand);
        }
         
        public void AddProductToShopcart(BizShopCartEntity entity)
        {
            string sql = @"IF NOT EXISTS (SELECT 1 FROM Biz_ShopCart WHERE ProductID=@ProductID AND CustomerID=@CustomerID)
BEGIN
    INSERT INTO Biz_ShopCart(CartID,ProductID,CustomerID,Quantity,CreatedAt,Deleted) VALUES (@CartID,@ProductID,@CustomerID,@Quantity,@CreatedAt,@Deleted)
END
";
            DbCommand dbcommand = CurrentDatabase.GetSqlStringCommand(sql);
            CurrentDatabase.AddInParameter(dbcommand, "@CartID", DbType.Guid, entity.CartID);
            CurrentDatabase.AddInParameter(dbcommand, "@ProductID", DbType.Guid, entity.ProductID);
            CurrentDatabase.AddInParameter(dbcommand, "@CustomerID", DbType.Guid, entity.CustomerID);
            CurrentDatabase.AddInParameter(dbcommand, "@Quantity", DbType.Decimal, entity.Quantity);
            CurrentDatabase.AddInParameter(dbcommand, "@CreatedAt", DbType.DateTime, entity.CreatedAt);
            CurrentDatabase.AddInParameter(dbcommand, "@Deleted", DbType.Boolean, entity.Deleted); 

            CurrentDatabase.ExecuteNonQuery(dbcommand);
        }

        /// <summary>
        /// 批量修改购物车信息
        /// </summary>
        /// <param name="OpType">0:insert,1:Update</param>
        /// <param name="list"></param>
        public void BatchInsert(int OpType,List<BizShopCartEntity> list)
        {
            DataTable table = new DataTable("Biz_ShopCart"); 
            table.Columns.Add("CartID", typeof(System.Guid));
            table.Columns.Add("ProductID", typeof(System.Guid));
            table.Columns.Add("CustomerID", typeof(System.Guid));
            table.Columns.Add("ProductCode", typeof(string));
            table.Columns.Add("Quantity", typeof(decimal));
            table.Columns.Add("Unit", typeof(string));
            table.Columns.Add("BatchCode", typeof(string));
            table.Columns.Add("DueDate", typeof(System.DateTime));
            table.Columns.Add("Inventory", typeof(System.Decimal));
            table.Columns.Add("Remark", typeof(string));
            table.Columns.Add("CreatedAt", typeof(System.DateTime));
            table.Columns.Add("Deleted", typeof(bool));
            foreach (BizShopCartEntity entity in list)
            {
                DataRow row = table.NewRow();
                row["CartID"] = entity.CartID;
                row["ProductID"] = entity.ProductID;
                row["CustomerID"] = entity.CustomerID;
                row["ProductCode"] = entity.ProductCode;
                row["Quantity"] = entity.Quantity;
                row["Unit"] = entity.Unit;
                row["BatchCode"] = entity.BatchCode;
                row["DueDate"] = entity.DueDate;
                row["Inventory"] = entity.Inventory;
                row["Remark"] = entity.Remark;
                row["CreatedAt"] = entity.CreatedAt;
                row["Deleted"] = entity.Deleted;
                table.Rows.Add(row);
            }

            DbCommand dbcommand = CurrentDatabase.GetStoredProcCommand("usp_ShopcartBatch");
         
            SqlCommand sqlcommand = dbcommand as SqlCommand;
            sqlcommand.Parameters.AddWithValue("@OpType", OpType);
            sqlcommand.Parameters.AddWithValue("@DataTable", table);

            if (Tran != null)
                CurrentDatabase.ExecuteNonQuery(sqlcommand, Tran);
            else
                CurrentDatabase.ExecuteNonQuery(sqlcommand);
        }
    }
}
